import log4js from 'log4js';

import logConf from '#Config/log.js';
import { empty } from '#Utils/helper.js';



class LogContainer {
    constructor() {
        if (LogContainer.instance) {
            return LogContainer.instance;
        }
        LogContainer.instance = this;
        this.initialized = false;
        this.log = log4js;
        this._Logger = new Map();
        this.init();
    }

    static getInstance() {
        if (!LogContainer.instance) {
            LogContainer.instance = new LogContainer();
        }
        return LogContainer.instance;
    }

    init() {
        if (this.initialized) {
            return;
        }
        
        this.log.configure(logConf);
        this.initialized = true;
    }

    channel(category = '') {
        if (empty(category)) {
            category = 'default';
        }

        if (!this._Logger.has(category)) {
            this._Logger.set(category, this.log.getLogger(category));
        }
        return this._Logger.get(category);
    }
}

export const LogFacade = LogContainer.getInstance();

export const Log = LogFacade.channel();